package com.yxh.common.handler;

import com.alibaba.fastjson.JSONException;
import com.yxh.exception.LoginException;
import com.yxh.exception.*;
import com.yxh.util.APIErrorCode;
import com.yxh.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartException;


/**
 * 全局异常处理类
 *
 * @author Sh1nno
 * @since 2018.3.7
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = LoginException.class)
    public Response baseErrorHandler(LoginException le) throws Exception {
        return Response.returnResponse(le.getCode());
    }

    @ExceptionHandler(value = WordbookException.class)
    public Response baseErrorHandler(WordbookException le) throws Exception {
        return Response.returnResponse(le.getCode());
    }

    @ExceptionHandler(value = MenuException.class)
    public Response baseErrorHandler(MenuException le) throws Exception {
        return Response.returnResponse(le.getCode());
    }

    @ExceptionHandler(value = UserException.class)
    public Response baseErrorHandler(UserException ue) throws Exception {
        return Response.returnResponse(ue.getCode());
    }

    @ExceptionHandler(value = ValidException.class)
    public Response baseErrorHandler(ValidException ve) throws Exception {
        return Response.returnResponse(ve.getCode());
    }

    @ExceptionHandler(value = AuthException.class)
    public Response baseErrorHandler(AuthException ae) throws Exception {
        return Response.returnResponse(ae.getCode());
    }

    @ExceptionHandler(value = DeptException.class)
    public Response baseErrorHandler(DeptException de) throws Exception {
        return Response.returnResponse(de.getCode());
    }

    @ExceptionHandler(value = RoleException.class)
    public Response baseErrorHandler(RoleException re) throws Exception {
        return Response.returnResponse(re.getCode());
    }

    @ExceptionHandler(value = WorkerException.class)
    public Response baseErrorHandler(WorkerException we) throws Exception {
        return Response.returnResponse(we.getCode());
    }

    @ExceptionHandler(value = ProjectException.class)
    public Response baseErrorHandler(ProjectException pe) throws Exception {
        return Response.returnResponse(pe.getCode());
    }

    @ExceptionHandler(value = TaskException.class)
    public Response baseErrorHandler(TaskException te) throws Exception {
        return Response.returnResponse(te.getCode());
    }

    @ExceptionHandler(value = UploadException.class)
    public Response baseErrorHandler(UploadException ue) throws Exception {
        return Response.returnResponse(ue.getCode());
    }

    @ExceptionHandler(value = Exception.class)
    public Response defaultErrorHandler(Exception e) throws Exception {
        if (e instanceof HttpRequestMethodNotSupportedException) {
            return Response.returnResponse(APIErrorCode.HTTPREQUESTMETHOD_NOTSUPPORTED);
        } else if (e instanceof HttpMediaTypeNotSupportedException) {
            return Response.returnResponse(APIErrorCode.HTTPMEDIATYPE_NOTSUPPORTED);
        } else if (e instanceof MultipartException) {
            return Response.returnResponse(APIErrorCode.CONTENTTYPE_ERROR);
        } else if (e instanceof JSONException) {
            return Response.returnResponse(APIErrorCode.JSON_ERROR);
        }
        logger.error("Runtime Exception:" + e.getMessage(), e);
        return Response.returnResponse(APIErrorCode.OPERATION_ERROR);
    }
}
